/**
 * Created by wqz806687102 on 2016/1/11.
 */
'use strict';

angular.module('Query').controller('SingleProductController', ['$scope', '$rootScope', '$state', 'BaseModule', 'BaseEnum', 'BaseFunction', '$location', 'Authentication', 'usbPort',
    function($scope, $rootScope, $state, BaseModule, BaseEnum, BaseFunction, $location, Authentication, usbPort) {
        $scope.QCStatusEnum = BaseFunction.getEnum('QCEnum.QCStatus');
        $scope.serachText; //关键字
        BaseModule.initModule($scope, 'BarcodeMaster');
        $scope.Module.BarcodeMaster.LoadMode = BaseEnum.LoadMode.CacheAndNoPaging;
        $scope.Module.BarcodeMaster.PageSize = 15;
        $scope.Module.BarcodeMaster.Populate = [{
            path: 'QCReworkOperation',
            select: 'Work',
            model: 'Operation',
            populate: {
                path: 'Work',
                select: 'Name',
                model: 'Work'
            }
        }, {
            path: 'Users',
            select: 'Code Name',
            model: 'User'
        }];

        BaseModule.initModule($scope, '_BarcodeMaster');
        $scope.Module._BarcodeMaster.LoadMode = BaseEnum.LoadMode.CacheAndNoPaging;
        $scope.Module._BarcodeMaster.PageSize = 15;

        $scope.$on('usbPort.input', function(e, inputStr) {
            $scope.serachText = inputStr;
            $scope.btnQuery();
        });

        $scope.$on('$destroy', function() {
            usbPort.stop();
        });

        $scope.init = function() {
            usbPort.start();
        };

        $scope.init();

        $scope.dbclick = function() {
            $rootScope.$broadcast('subMenuChange', {
                Name: '单品流水',
                notRolesMenu: true
            });
            $state.go('index.SingleProductStream', {
                Barcode: this.Module._BarcodeMaster.FocusedRecord.Barcode
            });
        };

        $scope.btnQuery = function() {
            if ($scope.serachText == undefined || $scope.serachText.trim() == '') {
                return;
            };

            var inputDate = RQcheck($scope.serachText);

            //查询条件--条码，作业，批次，物料，规格
            var condition = {};

            if (inputDate) {
                var startTime = BaseFunction.stringToDateTime($scope.serachText + ' 00:00:00');
                var endTime = BaseFunction.stringToDateTime($scope.serachText + ' 23:59:59');

                condition = {
                    $and: [{
                        CreatedOn: {
                            $gte: startTime
                        }
                    }, {
                        CreatedOn: {
                            $lte: endTime
                        }
                    }]
                };
            } else {
                condition = {
                    "$or": [{
                        Barcode: { $regex: $scope.serachText, $options: 'i' }
                    }, {
                        WorkName: { $regex: $scope.serachText, $options: 'i' }
                    }, {
                        MOLotNumber: { $regex: $scope.serachText, $options: 'i' }
                    }, {
                        ItemMasterName: { $regex: $scope.serachText, $options: 'i' }
                    }, {
                        Specification: { $regex: $scope.serachText, $options: 'i' }
                    }]
                };
            };


            this.Module.BarcodeMaster.find(condition, null, $scope.Module.BarcodeMaster.Populate, function(rcds) {
                var rds = [];
                for (var item in rcds) {
                    if (rcds[item].BarcodeType == 0) {
                        for (var user in rcds[item].Users) {
                            if (user == 0) {
                                rcds[item]["username"] = rcds[item].Users[user].Name;
                            } else {
                                rcds[item]["username"] += ',' + rcds[item].Users[user].Name;
                            };
                        };
                        rds.push(rcds[item]);
                    };
                };

                $scope.Module._BarcodeMaster.Records = rcds;
                $scope.Module._BarcodeMaster.emitOnLoadEvent();
            });
        };
    }
]);

//校验输入是否是yyyy-mm-dd日期格式
function RQcheck(RQ) {
    var date = RQ;
    var result = date.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);

    if (result == null)
        return false;
    var d = new Date(result[1], result[3] - 1, result[4]);
    return (d.getFullYear() == result[1] && (d.getMonth() + 1) == result[3] && d.getDate() == result[4]);
}
